﻿using Microsoft.AspNetCore.Mvc;
using MVC.Models;
using MVC.Models.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MVC.Controllers
{
    public class UpdateAController : Controller
    {
        private readonly LifengContext _context;
        public UpdateAController(LifengContext context)
        {
            _context = context;
        }
        [Route("/UpdateA")]

        public IActionResult UpdateA()
        {
            return View();
        }
        [HttpPost]
        [Route("/api/UpdateAdmin")]
        public IActionResult UpdateA(UpdateA request)
        {
            Random random = new Random();
            var userid = request.UserId;
            var username = request.UserName;
            var userpassword = request.UserPassword;
            var useremail = request.UserEmail;
            var user = _context.Users.FirstOrDefault(x => x.UserId == userid);
            if(user.UserId== userid) {
            _context.Update(new User
            {
                UserId= userid,
                UserName = username,
                UserPassword = userpassword,
                UserEmail = useremail
            });
            }
            _context.SaveChanges();
            return Json(new { code = 200, msg = "添加成功" });
        }   
    }
}
